/**
******************************************************************************************
* @file IOBouncer.c
* @brief Management of the io-16 bricklet and synchronize class which use it
* @date 14 mai 2015
* @author Benjamin Lancinot
*****************************************************************************************/
/* COPYRIGHT ****************************************************************************
*****************************************************************************************
* Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
*    list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************************/

#include "IOBouncer.h"

/*******************************************************************************************
* PROTOTYPES
********************************************************************************************/

void IOBouncer_initIo(IOBouncer*);

void IOBouncer_destroyUtilities(IOBouncer*);

void IOBouncer_destroy(IOBouncer **);

/*******************************************************************************************
* FUNCTIONS
********************************************************************************************/

/**
 * @brief Constructor
 * @param ipcon Ipconnection for link bricklet to the rest of bricks/bricklets
 * @return Pointer to a new IOBouncer
 **/
IOBouncer* IOBouncer_create(IPConnection ipcon){
	IOBouncer* This = ( IOBouncer*) malloc(sizeof(*This));
        IO16 io;
        io16_create(&io, "s5Y", &ipcon);

        This->io = io;
        This->initIo = IOBouncer_initIo;
	This->destroy = IOBouncer_destroy;
	return This;
}

/**
 * @brief Create LedsHandyman and DiscJOckey.Make ports of io-16 on good state (output with low value).
 * @param This Pointer to the wanted IOBouncer
 **/
void IOBouncer_initIo(IOBouncer* This){
        // Pin a0 to a7 in output with low value for leds
	io16_set_port_configuration(&This->io, 'a',255, 'o', false);
        //Pin b0 and b1 in output with low value for leds and b7 for music
	io16_set_port_configuration(&This->io, 'b',131, 'o', false);

        This->ledsHandyman = LedsHandyman_create(This->io);
        This->discJockey = DiscJockey_create(This->io);
}

/**
 * @brief Destroy all of the components of IOBouncer
 * @param This Pointer to the wanted IOBouncer
 **/
void IOBouncer_destroyUtilities(IOBouncer* This){

        //Destroy all objects which belonged to NannyBearBrain
        This->ledsHandyman->destroy(&This->ledsHandyman);
        This->discJockey->destroy(&This->discJockey);
        io16_destroy(&This->io);

}

/**
 * @brief Destructor
 * @param This Double pointer to the wanted IOBouncer
 **/
void IOBouncer_destroy(IOBouncer **This){
	IOBouncer_destroyUtilities(* This);
        if (*This) {
        	free(*This);
		*This = NULL;
	}
}
